package org.example.springbootblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.example.springbootblog.common.exception.BlogException;
import org.example.springbootblog.common.pojo.dataobject.BlogInfo;
import org.example.springbootblog.common.pojo.dataobject.UserInfo;
import org.example.springbootblog.common.pojo.request.UserLoginParam;
import org.example.springbootblog.common.pojo.response.UserInfoResponse;
import org.example.springbootblog.common.pojo.response.UserLoginResponse;
import org.example.springbootblog.common.pojo.utils.BeanConver;
import org.example.springbootblog.common.pojo.utils.JwtUtil;
import org.example.springbootblog.common.pojo.utils.SecurityUtil;
import org.example.springbootblog.mapper.BlogInfoMapper;
import org.example.springbootblog.mapper.UserInfoMapper;
import org.example.springbootblog.service.UserService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    //注入进来
    @Resource(name = "userInfoMapper")
    private UserInfoMapper userInfoMapper;
    @Resource(name = "blogInfoMapper")
    private BlogInfoMapper blogInfoMapper;
    @Override
    public UserLoginResponse login(UserLoginParam userParam) {
        //判断用户是否存在
        //根据用户名，去查询用户信息
        UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getUserName,userParam.getUserName())
                .eq(UserInfo::getDeleteFlag,0));
        if (userInfo == null) {
            //抛异常
            throw new BlogException("用户不存在");
        }

        //用户存在,检验密码是否正确
        if (!SecurityUtil.verify(userParam.getPassword(),userInfo.getPassword())) {
            throw new BlogException("密码错误");
        }
        //密码正确
        UserLoginResponse response = new UserLoginResponse();
        response.setUserId(userInfo.getId());
        //载荷  Jwt令牌
        Map<String,Object> map = new HashMap<>();
        map.put("id",userInfo.getId());
        map.put("name",userInfo.getUserName());
        response.setToken(JwtUtil.getToken(map));
        return response;
    }

    @Override
    public UserInfoResponse getUserInfoById(Integer userId) {
        UserInfo userInfo = selectUserInfoById(userId);
        return BeanConver.trans(userInfo);
    }

    @Override
    public UserInfoResponse getAuthorInfo(Integer blogId) {
        //1 根据博客id, 拿到作者id
        BlogInfo blogInfo = blogInfoMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId, blogId).eq(BlogInfo::getDeleteFlag, 0));
        //2 根据作者id,拿到作者详情
        if (blogInfo == null) {
            throw new BlogException("博客不存在");
        }
        UserInfo userInfo = selectUserInfoById(blogInfo.getUserId());
        return BeanConver.trans(userInfo);
    }

    public UserInfo selectUserInfoById(Integer userId) {
        return userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getId, userId).eq(UserInfo::getDeleteFlag, 0));
    }
}
